import { ref } from "vue";

const AMapInstance = window.AMap;
const addressList = ref([]);
export default (autoOptions, keyWord) => {
  return new Promise((resolve, reject) => {
    try {
      if (!AMapInstance) {
        reject("AMapInstance is not defined");
      }
      AMapInstance.plugin("AMap.PlaceSearch", function () {
        const placeSearch = new AMapInstance.PlaceSearch(autoOptions);
        placeSearch.search(keyWord, function (status, result) {
          const list = result?.poiList?.pois || [];
          addressList.value = list.map(item => {
            return {
              id: item.id,
              value: item.name,
              label: item.name
            };
          });
          resolve(addressList.value);
        });
      });
    } catch (err) {
      reject(err);
    }
  });
};
